﻿CREATE PROCEDURE [dbo].[proc_Statistics_Financial_CreateDaily_aotu]	
AS
Begin
	Declare @CreateDate1 DATETIME,@i INT,@temp NCHAR(10)

SET @CreateDate1=Convert(varchar(10),GETDATE()-1,120)+' 23:59:59'
SET @i=0

declare @TempSca table(d_str VARCHAR(100),d_str1 int )

--没有日报的单位 
INSERT INTO @TempSca(d_str,d_str1) SELECT c.CompanyName,c.Id FROM Company c WHERE id NOT IN 
(SELECT CompanyId FROM Statistics_Financial WHERE  Convert(varchar(10),CreateDate,120)=Convert(varchar(10),@CreateDate1,120) )

Insert Into Statistics_Financial (IBA,IBB,IBC,CreateUser,CreateDate,CompanyId,CompanyName) 
		SELECT iba,ibb,ibc,CreateUser,@CreateDate1,d_str1,d_str FROM Statistics_Financial,@TempSca 
		WHERE CompanyId=d_str1 and Convert(varchar(10),CreateDate,120)=Convert(varchar(10),DATEADD(DD,-1, @CreateDate1),120)

INSERT INTO gxrz (nr) SELECT '单位ID为'+CONVERT(VARCHAR,d_str1)+'，由程序转前一天数据;IBA：'
+CONVERT(VARCHAR,isnull(iba,0))+';ibb：' +CONVERT(VARCHAR,isnull(ibb,0))+';当日数据：IBA：'+CONVERT(VARCHAR,isnull(Statistics_Financial.iba,0))
+';IBB：'+CONVERT(VARCHAR,isnull(Statistics_Financial.IBB,0))+';IBC：'+CONVERT(VARCHAR,isnull(Statistics_Financial.IBC,0))
 FROM Statistics_Financial,@TempSca WHERE CompanyId=d_str1 and Convert(varchar(10),CreateDate,120)=Convert(varchar(10),DATEADD(DD,-1, @CreateDate1),120)



SET @temp=Convert(nchar(10),@CreateDate1,120) 
EXEC proc_Statistics_Financial_Tongji_Create
	@CreateDate = @temp
End
	RETURN